package com.bayi.storage.network;

import java.lang.reflect.Constructor;
import java.util.HashMap;

/**
 * Created by guoping on 16/8/28.
 */
public class SingletonManager {

    private static HashMap<Class, Object> instances = new HashMap<>();

    public synchronized static <T> T getInstance(Class classOfT) {
        if (!instances.containsKey(classOfT)) {
            try {
                Constructor constructor = classOfT.getConstructor();
                constructor.setAccessible(true);
                constructor.newInstance();
                instances.put(classOfT, constructor.newInstance());
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
        return (T) instances.get(classOfT);
    }

    public synchronized static void remove(Class clazz) {
        if (instances.containsKey(clazz)) {
            instances.remove(clazz);
        }
    }
}
